// 对外部空值做处理
class MayBe {
  static of(value) {
    return new MayBe(value)
  }
  constructor(value) {
    this._value = value
  }
  map(fn) {
    // 处理方法是返回一个值为null的函子
    return this.isNothing() ? MayBe.of(null) : MayBe.of(fn(this._value))
  }
  isNothing() {
    return this._value === null || this._value === undefined
  }
}

let r1 = MayBe.of('Hello World').map((x) => x.toUpperCase)
console.log(r1)
// MayBe { _value: [Function: toUpperCase] }
let r2 = MayBe.of(null).map((x) => x.toUpperCase)
console.log(r2)
// MayBe { _value: null }

// 问题

let r3 = MayBe.of('Hello World')
  .map((x) => x.toUpperCase)
  .map((x) => x.split(' '))
console.log(r3)
// MayBe { _value: null }
// 不知道什么时候出现了null! 无法定位
